<!-- 搜索商品的展示页 -->
<template>
  <div class="search-results-container">
    <HeadNav></HeadNav>
    <!-- 添加 isInSearchPage 属性 -->
    <SearchBar 
      :initial-keyword="keyword" 
      :isInSearchPage="true"
      @search="handleSearch">
    </SearchBar>
    
   
    <h2 class="search-title">"{{ keyword }}"的搜索结果</h2>
    
    <div v-if="loading" class="loading-box">
      <el-skeleton :rows="6" animated />
    </div>
    
    <div v-else-if="results.length === 0" class="no-results">
      <el-empty description="没有找到相关商品"></el-empty>
    </div>
    
    <div v-else class="product-list">
      <div 
        v-for="product in results" 
        :key="product.product_id" 
        class="product-item"
        @click="goToDetail(product.product_id)">
        <div class="product-image">
          <img :src="getImageUrls(product.image)" :alt="product.name">
        </div>
        <div class="product-info">
          <h3 class="product-name">{{ product.name }}</h3>
          <p class="product-price">¥{{ product.price.toFixed(2) }}</p>
          <p class="product-desc">{{ product.description || '暂无描述' }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import HeadNav from '../components/HeadNav.vue'
import SearchBar from '../components/SearchBar.vue'
import request from '@/utils/request' // 导入封装好的request

export default {

  components: {
    HeadNav,
    SearchBar
  },
  name: 'SearchResults',
  data() {
    return {
       keyword: this.$route.query.keyword || '',
            results: this.$route.params.results || [],
      loading: false
    }
  },
  created() {
    if (!this.$route.params.fromSearch && this.keyword) {
      this.loadSearchResults()
    }
  },
  methods: {
    getImageUrls(relativePaths) {
      return require(`@/assets/img/goods/${relativePaths}`)
    },
    
    handleSearch(keyword) {
      this.keyword = keyword
      this.loadSearchResults()
    },
    
    loadSearchResults() {
      if (!this.keyword) {
        this.results = []
        return
      }
      
      this.loading = true
      
      // 使用封装好的request
      request.get(`/products/findgoods/${encodeURIComponent(this.keyword)}`)
        .then(res => {
          if (res.code === '200') {
            this.results = res.data || []
            this.$router.replace({
              name: 'SearchResults',
              query: { keyword: this.keyword },
              params: {
                results: this.results,
                fromSearch: true
              }
            }).catch(() => {})
          } else {
            this.$message.error(res.msg || '搜索失败')
          }
        })
        .catch(error => {
          console.error('搜索失败:', error)
          this.$message.error('搜索失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    
  goToDetail(productId) {
    this.$router.push({
      name: 'ProductDetail',
      params: { product_id: productId } // 统一使用product_id
    })
  }
  }
}
</script>

<style scoped>
.search-results-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 15px;
}

.search-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.product-item {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.product-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.product-image {
  height: 200px;
  overflow: hidden;
}

.product-image img {
 width: 100%;
 height: auto;
 max-height: 160px;
 object-fit: contain;
 padding-top: 30px;
}

.product-item:hover .product-image img {
  transform: scale(1.05);
}

.product-info {
  padding: 15px;
  text-align: center;
}

.product-name {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-price {
  font-size: 18px;
  color: #f56c6c;
  font-weight: bold;
  margin-bottom: 8px;
}

.product-desc {
  font-size: 14px;
  color: #999;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.no-results {
  margin-top: 50px;
  text-align: center;
}

.loading-box {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
}
</style>